import wx from 'jweixin-module'

export default {
    initJSSDK(config) {
        return new Promise((resolve, reject) => {
            console.log('config', config)
            wx.config({
                debug: false,
                appId: config.appId,
                timestamp: config.timestamp,
                nonceStr: config.nonceStr,
                signature: config.signature,
                jsApiList: [
                    'chooseWXPay',
                    'updateAppMessageShareData',
                    'updateTimelineShareData',
                    'onMenuShareAppMessage',
                    'onMenuShareTimeline',
                ]
            })
            console.log('微信JSSDK初始化')
            wx.ready(() => {
                console.log('微信JSSDK初始化成功')
                resolve()
            })

            wx.error(err => {
                console.error('微信JSSDK初始化失败:', err)
                reject(err)
            })
        })
    },

    // 分享给朋友
    shareToFriend(shareData) {
        return new Promise((resolve, reject) => {
            if (!shareData.title || !shareData.link) {
                reject('分享参数不完整：缺少标题或链接')
                return
            }
            wx.updateAppMessageShareData({
                title: shareData.title,
                desc: shareData.desc || '',
                link: shareData.link,
                imgUrl: shareData.imgUrl || '',
                success: function() {
                    console.log('分享给朋友设置成功')
                    resolve('分享成功')
                },
                cancel: function() {
                    console.log('用户取消分享')
                    reject('取消分享')
                },
                fail: function(err) {
                    console.error('分享给朋友失败:', err)
                    const errorMsg = err.errMsg || '分享失败'
                    reject(`分享失败: ${errorMsg}`)
                }
            })
        })
    },

    // 分享到朋友圈
    shareToTimeline(shareData) {
        return new Promise((resolve, reject) => {
            wx.updateTimelineShareData({
                title: shareData.title, // 分享标题
                link: shareData.link, // 分享链接
                imgUrl: shareData.imgUrl, // 分享图标
                success: function() {
                    console.log('分享到朋友圈成功')
                    resolve('分享成功')
                },
                cancel: function() {
                    console.log('用户取消分享')
                    reject('取消分享')
                },
                fail: function(err) {
                    console.error('分享到朋友圈失败:', err)
                    const errorMsg = err.errMsg || '分享失败'
                    reject(`分享失败: ${errorMsg}`)
                }
            })
        })
    },

    // 设置分享信息（同时设置朋友和朋友圈）
    setShareData(shareData) {
        return new Promise((resolve, reject) => {
            // 设置分享给朋友
            wx.updateAppMessageShareData({
                title: shareData.title,
                desc: shareData.desc,
                link: shareData.link,
                imgUrl: shareData.imgUrl,
                success: function() {
                    console.log('设置分享给朋友成功')
                    resolve('分享设置成功')
                },
                cancel: function() {
                    console.log('用户取消分享')
                    reject('取消分享设置')
                },
                fail: function(err) {
                    console.error('分享给朋友失败:', err)
                    const errorMsg = err.errMsg || '分享失败'
                    reject(`分享失败: ${errorMsg}`)
                }
            })

            // 设置分享到朋友圈
            wx.updateTimelineShareData({
                title: shareData.title,
                link: shareData.link,
                imgUrl: shareData.imgUrl,
                success: function() {
                    console.log('设置分享到朋友圈成功')
                    resolve('分享设置成功')
                },
                cancel: function() {
                    console.log('用户取消分享')
                    reject('取消分享设置')
                },
                fail: function(err) {
                    console.error('分享到朋友圈失败:', err)
                    const errorMsg = err.errMsg || '分享失败'
                    reject(`分享失败: ${errorMsg}`)
                }
            })
        })
    },

    requestPayment(payInfo) {
        return new Promise((resolve, reject) => {
            wx.chooseWXPay({
                timestamp: payInfo.timeStamp,
                nonceStr: payInfo.nonceStr,
                package: payInfo.package,
                signType: payInfo.signType,
                paySign: payInfo.paySign,
                success: (res) => {
                    resolve(res)
                },
                fail: (err) => {
                    reject(err)
                }
            })
        })
    }
}